﻿$("#TestedByName").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "/Project/AutoCompleteProjectMember",
            type: "POST",
            dataType: "json",
            data: {
                Keyword: request.term,
                DefectId: $("#DefectId").val()
            },
            success: function (Accounts) {
                response($.map(Accounts, function (Account) {
                    return {
                        label: Account.FirstName + " " + Account.LastName + " - " + Account.Email,
                        value: Account.FirstName + " " + Account.LastName,
                        AccountId: Account.Id
                    }
                }));
            }
        });
    },
    minLength: 1,
    select: function (event, ui) {
        $("#TestedBy").val(ui.item.AccountId);
    },
    open: function () {        
        $("#TestedBy").val(null);
        $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
    },
    close: function () {
        $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
    }
});

$("#TestedByName").focusout(function () {
    if ($("#TestedBy").val() == null || $("#TestedBy").val() == "") {
        $(this).val("");
    }
    if ($(this).val() == "" || $(this).val() == null) {
        $("#TestedBy").val(null);
    }
});

$("#TestedByName").keyup(function (event) {
    $("#TestedBy").val(null);
    event.preventDefault();
});

$("#loading").ajaxStart(function () {
    $(this).center();
    $(this).show();
});

$("#loading").ajaxStop(function () {
    $(this).center();
    $(this).hide();
});